package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.Shopping;
import cn.cs.yangtai.entity.vo.CommodityVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.entity.vo.ShoppingVo;
import cn.cs.yangtai.service.ShoppingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Shopping)表控制层
 *
 * @author makejava
 * @since 2024-12-12 09:22:01
 */
@RestController
@CrossOrigin
@RequestMapping("shopping")
@Tag(name = "购物车")
public class ShoppingController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppingService shoppingService;

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @GetMapping
    public ResponsePojo<PageResult<ShoppingVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<ShoppingVo> result=shoppingService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<ShoppingVo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.shoppingService.queryById(id));
    }

    /**
     * 查询个人购物车
     * @return 购物车列表
     */
    @Operation(summary = "个人购物车")
    @GetMapping("cart")
    public ResponseEntity<ResponsePojo<List<ShoppingVo>>> queryBysUserid(@RequestParam("sUserid") Integer sUserid) {
        List<ShoppingVo> shoppings = shoppingService.queryBysUserid(sUserid);
        return ResponseEntity.ok(ResponsePojo.success(shoppings));
    }
    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Shopping> add(Integer sUserid,Integer sFType,String sCName,Integer sQuantity,Double sPrice) {
        Shopping shopping=new Shopping();
        shopping.setSUserid(sUserid);
        shopping.setSFType(sFType);
        shopping.setSCName(sCName);
        shopping.setSQuantity(sQuantity);
        shopping.setSPrice(sPrice);
        return ResponseEntity.ok(this.shoppingService.insert(shopping));
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<ShoppingVo> edit(Integer sId,Integer sQuantity,Double sPrice) {
        Shopping shopping=new Shopping();
        shopping.setSId(sId);
        shopping.setSQuantity(sQuantity);
        shopping.setSPrice(sPrice);
        return ResponseEntity.ok(this.shoppingService.update(shopping));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.shoppingService.deleteById(id));
    }

}

